﻿/*
    drifter-video-screensaver
    Copyright (C) 2013  Matt Lunsford

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace DrifterVideoScreensaver
{

	public partial class VideoDisplay : Form
	{
		Point InitialMouse;

		int ScreenNumber;

		bool thisIsPrimary;
		
		System.Diagnostics.Process theProcess;

		public VideoDisplay()
		{
			InitializeComponent();
			this.ScreenNumber = 0;
		}

		public VideoDisplay( int ScreenNum )
		{
			InitializeComponent();
			this.ScreenNumber = ScreenNum;
		}

		private void onLoad(object sender, EventArgs e)
		{

            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Drifter_Video_Screensaver");

            if (!Directory.Exists((string)key.GetValue("videofolder")))
            {
                //message that playlist folder is not set correctly, exit
                MessageBox.Show("Playlist folder is not set, please set it in the screensaver settings. Exiting.");
                Application.Exit();
            }

            if (!File.Exists((string)key.GetValue("vlclocation")))
            {
                //message that vlc location is not set correctly, exit
                MessageBox.Show("Location of VLC is not set, please set it in the screensaver settings. Exiting.");
                Application.Exit();
            }

			Cursor.Hide();

			this.thisIsPrimary = Screen.AllScreens[this.ScreenNumber].Primary;

			if (this.thisIsPrimary == false)
			{
				/* Not the primary monitor */
				this.BackColor = Color.Black;
				this.Bounds = Screen.AllScreens[this.ScreenNumber].Bounds;
				this.BringToFront();
			}
			else
			{
				this.Capture = true;
				this.Focus();
				this.TopMost = true;

				this.BackColor = Color.Black;
				this.TransparencyKey = Color.Black;

				this.Show();
				this.SetDesktopLocation(Screen.PrimaryScreen.Bounds.X + 20, Screen.PrimaryScreen.Bounds.Y + 20);

                String ArgumentList = " \"" + (string)key.GetValue("videofolder") + "\" ";
				ArgumentList += " --video-on-top -f -L ";
                if ((string)key.GetValue("noaudio") == "True")
				{
					ArgumentList += " --no-audio";
				}
                if ((string)key.GetValue("randomplay") == "True")
				{
					ArgumentList += " -Z";
				}
				this.theProcess = new System.Diagnostics.Process();
				this.theProcess.StartInfo.FileName = "vlc.exe";
				this.theProcess.StartInfo.Arguments = ArgumentList;
                this.theProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName((string)key.GetValue("vlclocation"));

				this.theProcess.Start();

				this.timer1.Enabled = true;
			}
		}

		private void OnMouseMove(object sender, MouseEventArgs e)
		{
			if (!InitialMouse.IsEmpty)
			{
				if (InitialMouse != new Point(e.X, e.Y))
				{
					this.KillVLC();
					this.Close();
				}
			}
			else
			{
				InitialMouse = new Point(e.X, e.Y);
			}
		}

		private void OnKeypress(object sender, KeyPressEventArgs e)
		{
			this.KillVLC();
			this.Close();
		}

		private void OnMouseClick(object sender, MouseEventArgs e)
		{
			this.KillVLC();
			this.Close();
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			if (!InitialMouse.IsEmpty)
			{
				if (InitialMouse != Cursor.Position)
				{
					this.KillVLC();
					this.Close();
				}
			}
			else
			{
				InitialMouse = new Point(Cursor.Position.X, Cursor.Position.Y);
			}
		}

		private void KillVLC()
		{
			if (this.theProcess.HasExited == false)
			{
                //THIS SECTION NEEDS TO EXIT GRACEFULLY!
                this.theProcess.Kill();
                this.theProcess.WaitForExit();
           
			}
		}

	}
}
